package Cain.save 
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	/**
	 * 載入本機圖檔
	 * @author cain in liontravel 2010
	 */
	public class LoadPcImage extends Loader
	{
		/**
		 * 選圖完成
		 */
		static public const SelectComplete:String = "LoadPcImage_SelectComplete";
		private var mydata:ByteArray;
		private var _file		:FileReference = new FileReference();
		/**
		 * 載入本機圖檔
		 */
		public function LoadPcImage() 
		{
			super();
			_file.addEventListener(Event.SELECT, fileSelectHandler);
			//當檔案被選取時。
			_file.browse([new FileFilter("Images Formats (*.jpg,*.gif,*.png)", "*.jpg;*.gif;*.png", "JPEG;jp2_;GIFF")]);
		}
		/**
		 * 取得圖形資料
		 */
		public function get ImageByte():ByteArray 
		{
			return mydata;
		}
		private function fileSelectHandler(e:Event):void 
		{
			_file.removeEventListener(Event.SELECT, fileSelectHandler);
			_file.addEventListener(Event.COMPLETE, fileCompleteHandler);
			//當檔案被成功載入時。	
			_file.load(); //載入。
		}
		
		private function fileCompleteHandler(e:Event):void 
		{
			_file.removeEventListener(Event.COMPLETE, fileCompleteHandler);
			mydata = _file.data; 
			this.loadBytes(mydata);
			dispatchEvent(new Event(SelectComplete));
		}
	}

}